package com.anyi.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.anyi.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.anyi.gulimall.product.entity.AttrEntity;
import com.anyi.gulimall.product.service.AttrAttrgroupRelationService;
import com.anyi.gulimall.product.vo.AttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.anyi.gulimall.product.entity.AttrGroupEntity;
import com.anyi.gulimall.product.service.AttrGroupService;
import com.anyi.gulimall.common.utils.PageUtils;
import com.anyi.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author anyi
 * @email 1961741003@qq.com
 * @date 2022-07-27 10:58:39
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{groupId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("groupId") Long categoryId){
        PageUtils page = attrGroupService.queryAttrGroup(params,categoryId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getAttrDetail(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 根据分类查出所有关联的分组，并且查出所有关联的属性
     */
    @RequestMapping("/{catelogId}/withattr")
    // @RequiresPermissions("product:attrgroup:info")
    public R infoWithAttr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupVo> attrGroups = attrGroupService.getInfoWithAttr(catelogId);
        return R.ok().put("data", attrGroups);
    }




    /**
     * 查询分组关联的属性
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    // @RequiresPermissions("product:attrgroup:info")
    public R getAttrGroupList(@PathVariable("attrgroupId") Long attrGroupId){
        List<AttrEntity> list = attrGroupService.getAttrGroupList(attrGroupId);
        return R.ok().put("data", list);
    }

    /**
     * 查询分组没有关联的信息
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    // @RequiresPermissions("product:attrgroup:info")
    public R getNoAttrGroupList(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrGroupService.getNoAttrGroupList(params,attrGroupId);
        return R.ok().put("data", page);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelation){
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = Arrays.asList(attrAttrgroupRelation);
        attrAttrgroupRelationEntities.forEach(item->{
            attrAttrgroupRelationService.remove(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                    .eq(AttrAttrgroupRelationEntity::getAttrGroupId,item.getAttrGroupId())
                    .eq(AttrAttrgroupRelationEntity::getAttrId,item.getAttrId()));
        });
        return R.ok();
    }



    @PostMapping("/attr/relation")
    // @RequiresPermissions("product:attr:delete")
    public R saveRelation(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelation){
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = Arrays.asList(attrAttrgroupRelation);
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }
}
